package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.CarRechargeFlagEnum;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.service.ICarRechargeService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.CarRechargeVO;
import cn.sykj.car.vo.ResponseMessageVO;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 充值记录 前端控制器
 * </p>
 *
 * @author yuhao
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/carRecharge")
@Slf4j
public class CarRechargeController extends BaseController {

    @Resource
    ICarRechargeService iCarRechargeService;

    /**
     * 充值
     *
     * @param carRecharge
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "充值", notes = "充值")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carRecharge:create")
    @RequestMapping(value = "/addRecharge", method = RequestMethod.POST)
    public ResponseBean addRecharge(@RequestBody CarRechargeVO carRecharge) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        carRecharge.setCreateTime(new Date());
        carRecharge.setCreateUser((String) obj);
        carRecharge.setUpdateTime(new Date());
        carRecharge.setUpdateUser((String) obj);
        carRecharge.setDelFlag(DeleteFlagEnum.FALSE.VALUE);

        // 管理端普通充值
        carRecharge.setRechargeFlag(CarRechargeFlagEnum.NORMAL_RECHARGE.VALUE);

        ResponseMessageVO responseMessageVO = iCarRechargeService.addRecharge(carRecharge);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
